<?php  
class LoginController extends Controller{
	private $db;

	public function __init(){
		$this->db=K("Login");
	}

    
    /**
     * 用户注册
     * @return [type] [description]
     */
    public function reg(){
        if (IS_POST) {
            if ($this->db->reg()) {
                $this->success('注册成功',__WEB__);
            }else{
                $this->error($this->db->error);
            }
        }else{
          $this->display();   
      }
    }
    /**
     * 用户登录
     * @return [type] [description]
     */
    public function login(){
        if (IS_POST) {
            if ($this->db->login()) {
               $this->success('登陆成功',__WEB__);
            }else{
                $this->error($this->db->error);
            }
        }else{
            $this->display();
        }
    }

    /**
     * 显示验证码
     * @return [type] [description]
     */
    public function code(){
        $db=new Code;
        $db->show();
    }
    /**
     * 前台退出
     * @return [type] [description]
     */
    public function out(){
        unset($_SESSION['user']);
        $this->success('退出成功');
    }
    /**
     * 异步严验证登录验证码是否正确
     * @return [json] [返回状态和提示信息]
     */
    public function checkCode(){
        if ($_SESSION['code']!=strtoupper($_POST['code'])) {
            echo json_encode(array('status'=>0,'msg'=>'验证码错误'));exit;
        }else{
            echo json_encode(array('status'=>1,'msg'=>'正确'));exit;
        }
    }
    /**
     * 异步验证用户名
     * @return [json] [状态和提示信息]
     */
    public function checkUsername(){
        $user=$this->db->where("username=".$_POST['username'])->find();
        if (empty($user)) {
            echo json_encode(array('status'=>0,'msg'=>'用户不存在'));exit;
        }else{
            echo json_encode(array('status'=>1,'msg'=>'用户不正确'));exit;
        }
    }    
}
?>